Field   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 79
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 15

6 Functions

Rating   Name   Duplication   Size   Complexity  
C resetDirty 0 4 10
A setName 0 3 1
A tableSetup 0 3 1
A setParentProperties 0 3 1
A setup 0 4 1
A toJSON 0 5 1
1
import {ModelInterface, TableInterface} from "../../JeloquentInterfaces";
2
3
export default class Field {
4
5
    protected $fieldValue: unknown;
6
7
    protected $name: string;
8
9
    protected $originalValue: unknown;
10
11
    protected $parent: ModelInterface;
12
13
    protected $previousValue: unknown;
14
15
    private _isPrimary: boolean;
16
17
    /**
18
     *
19
     * @param name
20
     * @param isPrimary
21
     */
22
    constructor(name: string, isPrimary = false) {
23
        this._isPrimary = isPrimary;
24
        this.$fieldValue = null;
25
        this.$previousValue = undefined;
26
        this.$originalValue = undefined;
27
        this.$parent = null;
28
        this.$name = name;
29
    }
30
31
    get isDirty(): boolean {
32
        return this.$fieldValue != this.$previousValue;
33
    }
34
35
    get isPrimary(): boolean {
36
        return this._isPrimary;
37
    }
38
39
    get name(): string {
40
        return this.$name;
41
    }
42
43
    get originalValue(): unknown {
44
        return this.$originalValue;
45
    }
46
47
    get previousValue(): unknown {
48
        return this.$previousValue;
49
    }
50
51
    get value(): unknown {
52
        return this.$fieldValue;
53
    }
54
55
    set _value(newValue: unknown) {
56
        if (this.$originalValue === undefined) {
57
            this.$originalValue = JSON.parse(JSON.stringify(this.value ?? newValue));
58
        }
59
        this.$previousValue = JSON.parse(JSON.stringify(this.value));
60
        this.$fieldValue = newValue;
61
    }
62
63
64
    // eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
65
    set value(newValue: unknown) {
66
        if (this.$previousValue === undefined) {
67
            this.$previousValue = JSON.parse(JSON.stringify(this.value ?? newValue));
68
        }
69
70
        if (this.$originalValue === undefined) {
71
            this.$originalValue = JSON.parse(JSON.stringify(this.value ?? newValue));
72
        }
73
74
        this.$previousValue = JSON.parse(JSON.stringify(this.value));
75
76
        this.$fieldValue = newValue;
77
    }
78
79
    resetDirty(): void {
80
        this.$originalValue = JSON.parse(JSON.stringify(this.$fieldValue));
81
        this.$previousValue = JSON.parse(JSON.stringify(this.$fieldValue));
82
    }
83
84
    setName(): Field {
85
        return this;
86
    }
87
88
    setup(parent: ModelInterface): Field {
89
        this.$parent = parent;
90
        return this.setName().setParentProperties();
91
    }
92
93
    tableSetup(table: TableInterface): void {
94
        console.info(table.name);
95
    }
96
97
    toJSON(): object {
98
        const object = {};
99
        object[this.$name] = this.value;
100
        return object;
101
    }
102
103
    protected setParentProperties(): Field {
104
        return this;
105
    }
106
}